# -*- coding: utf-8 -*-
__author__ = 'lztkdr'
__date__ = '2019/7/22 15:25'


import requests
import json

"""
收费代理：http://http.zhiliandaili.com/
免费代理：http://ip.zdaye.com/dayProxy/ip/314181.html

"""

import  time

def getProxyHost():
	while True:
		try:
			# 使用 时效为1分钟 代理
			get_proxy_url = "http://t.11jsq.com/index.php/api/entry?method=proxyServer.generate_api_url&packid=0&fa=0&fetch_key=&groupid=0&qty=1&time=100&pro=&city=&port=1&format=json&ss=5&css=&ipport=1&dt=1&specialTxt=3&specialJson="
			json_data = json.loads(requests.get(get_proxy_url).content)
			data = json_data.get("data", None)
			if (not isinstance(data, list)) or not len(data):
				print(f"获取代理失败：{json_data}")
				return None
			else:
				proxy_host = data[0].get("IP", None)
				resp = requests.get(url="http://httpbin.org/get",params = None,\
				                proxies={"http": "http://"+proxy_host,"https": "https://"+proxy_host}) # requests 使用代理 字典的值 可以是 http:// 也可以直接是 ip:port
				if resp.status_code == 200:
					print(f"代理可用：{proxy_host}")
					return proxy_host
				else:
					print("代理不可用，继续获取...."+resp.text)
					time.sleep(1)
		except Exception as ex:
			print(ex)
			time.sleep(2)
			continue


